package com.example.fp.cgc.util

import kotlin.math.min

/**
 * @since 2020-02-25
 * @author 蔡
 */
object PageUtil {
    data class Page(
            var page: Int,
            var maxPage: Int,
            var currLoc: Int,
            var currMaxPage: Int
    )

    fun getPage(pageD: Int, count: Int, pageSize: Int): Page {
        var page: Int = pageD
        if (page == 0)
            page = 1

        val maxPage = if (count % pageSize == 0) count / pageSize else count / pageSize + 1
        if (page > maxPage) {
            page = maxPage
        }

        val currLoc = (page - 1) * pageSize

        val currMaxPage = min(page + pageSize, maxPage)

        return Page(page, maxPage, currLoc, currMaxPage)
    }
}